package com.whz.workmanageol.controller;

import com.whz.workmanageol.entity.User;
import com.whz.workmanageol.utils.FileUpload;
import com.whz.workmanageol.utils.MutilFilesDownload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
 * 教师用于下载学生提交的作业
 */

@Controller
@RequestMapping("/teacher")
public class DownWorkController {
    @GetMapping("/downWorks")
    public void downWork(HttpServletRequest request, HttpServletResponse response) {
        String gradeid = request.getParameter("gradeid");
        String courseid = request.getParameter("courseid");
        User user = (User) request.getSession().getAttribute("user");
        String teacherid = user.getId();
        String dirPath = FileUpload.FILE_UPLOAD_PATH + File.separator + teacherid + File.separator + gradeid + File.separator + courseid;
        String zipName = "作业集合" + ".zip";
        File fileZip = new File(zipName);
        List<File> fileList = MutilFilesDownload.traveseFolder(dirPath);
        try {
            FileOutputStream outputStream = new FileOutputStream(fileZip);
            ZipOutputStream toClient = new ZipOutputStream(outputStream);
            MutilFilesDownload.zipFile(fileList, toClient);
            toClient.close();
            outputStream.close();
            MutilFilesDownload.downloadFile(fileZip, response, true);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
